# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Maintainer: Joan Karadimov <joan.karadimov@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=pywin32
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
pkgver=308
pkgrel=3
pkgdesc='Python for Windows Extensions (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/mhammond/pywin32'
msys2_references=(
  'purl: pkg:pypi/pywin32'
)
license=('spdx:PSF-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             $([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "binutils")
             "${MINGW_PACKAGE_PREFIX}-cc")
source=(https://github.com/mhammond/pywin32/archive/b${pkgver}/${_realname}-${pkgver}.tar.gz
        001-compile-and-setup-fixes.patch
        002-com-extensions-fix.patch
        003-win32-extensions-fix.patch
        004-isapi-fix.patch
        005-swig-fix.patch
        006-mapi-headers-fix.patch)
sha256sums=('8aa09d39739764bec2378cfbd45940264c8b7f5a24caa17f04a01f7b750799ce'
            '6cf1981d8484b9874891b582e3c71ba9409d6c180a6f4e7fd84ec3307bfd746e'
            'af050c9c1e9c92c5ed3be73b606a4b931c78d6bb01175ecf5db503ba688d64ba'
            '0e4aadf7b6dec6baf1c347664396787110ad756d53720f2c1bb0eba48c733169'
            '0d303188a4a34759e4be1c2e11d68ba33f40edaf3056656618490c983d932dff'
            'cfe20ab0034b7f872d96363ea85261430f6a64ae403831978bd433c20415566c'
            '14c7c6db61fcef7fa799770874bc4cad464fb8c08495a56c9cf5beb795ba522b')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}-b${pkgver}"

  apply_patch_with_msg \
    001-compile-and-setup-fixes.patch \
    002-com-extensions-fix.patch \
    003-win32-extensions-fix.patch \
    004-isapi-fix.patch \
    005-swig-fix.patch \
    006-mapi-headers-fix.patch
}

build() {
  cp -r "${_realname}-b${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 "win32/License.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE.txt"
}
